package com.ztom.dp;

/**
 * 斐波那契数
 * <p>
 * https://leetcode-cn.com/problems/fibonacci-number/
 *
 * @author ZhangTao
 */
public class Code01Fib {

    public int fib2(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }

        return fib2(n - 1) + fib2(n - 2);
    }

    public int fib1(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }
        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }

        return dp[n];
    }

    public int fib(int n) {
        if (n == 0) {
            return 0;
        }

        int prepre = 0;
        int pre = 1;
        int res = 1;
        for (int i = 2; i <= n; i++) {
            res = pre + prepre;
            prepre = pre;
            pre = res;
        }

        return res;
    }
}
